﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 查询操作
    /// </summary>
    public class QueryHelper
    {
        /// <summary>
        /// 获取当前视图范围内所有Feature。若获取的总数大于允许取的总数，则只返回总数，不返回数据
        /// </summary>
        /// <param name="mapControl">地图控件</param>
        /// <param name="features">获取的空间对象集合</param>
        /// <param name="maxcount">允许获取的最大总数，默认为0，不限制</param>
        /// <returns></returns>
        public static int GetFeatureInCurrentView(IMapControl2 mapControl, out List<IFeature> features, int maxcount = 0)
        {
            features = new List<IFeature>();
            mapControl.Map.ClearSelection();
            IGeometry geo = mapControl.ActiveView.Extent.ToPolygon() as IGeometry;
            mapControl.Map.SelectByShape(geo, null, false);
            int featNum = mapControl.Map.SelectionCount;
            if (maxcount > 0 && featNum > maxcount)
            {
                mapControl.Map.ClearSelection();
                mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                return featNum;
            }
            IEnumFeature enumFeat = mapControl.Map.FeatureSelection as IEnumFeature;
            if (enumFeat != null)
            {
                IFeature feat = enumFeat.Next();
                while (feat != null)
                {
                    features.Add(feat);
                    feat = enumFeat.Next();
                }
            }
            mapControl.Map.ClearSelection();
            mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
            return features.Count;
        }

        /// <summary>
        /// 使用Identify获取图层中与空间相交的空间对象集合
        /// </summary>
        /// <param name="layer">要查询的图层</param>
        /// <param name="geometry">空间查询范围</param>
        /// <returns>IArray为IFeature对象的集合</returns>
        public static IArray IdentifyLayer(ILayer layer, IGeometry geometry)
        {
            if (geometry == null || geometry.IsEmpty) return null;
            if (layer is IFeatureLayer)
                return ((layer as IFeatureLayer) as IIdentify).Identify(geometry);
            else if (layer is IBasemapSubLayer)
                return ((layer as IBasemapSubLayer) as IIdentify).Identify(geometry);
            else if (layer is IRasterLayer || layer is IRasterCatalogLayer || layer is IGdbRasterCatalogLayer)
            {
                IPoint point = null;
                if (geometry is IPoint)
                    point = (geometry as IClone).Clone() as IPoint;
                else if (geometry is IEnvelope)
                    point = (geometry as IEnvelope).UpperLeft;
                else
                {
                    IPointCollection points = geometry as IPointCollection;
                    if (points == null || points.PointCount == 0) return null;
                    point = points.get_Point(0);
                }
                return (layer as IIdentify).Identify(point);
            }
            return null;
        }

    }
}
